#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#ifndef __cplusplus
#define bool  int
#define true  1
#define false 0
#endif


typedef struct Node
{
	int data;
	struct Node *next;
}NODE,*PNODE;

PNODE create_list(int);
traverse_list(PNODE);
bool empty_list(PNODE);
int get_list_node(PNODE);	// calc the num node

int main(void)
{
	int len;
	int cnt = 0;
	PNODE p_head;

	printf("pls input the num of node which you will create: len = ");
	scanf("%d", &len);

	p_head = create_list(len);
	if(empty_list(p_head))
	{
		printf("the list is empty!\n");
	}
	else
	{
		printf("the list is not empty!\n");
	}
	
	cnt = get_list_node(p_head);
	printf("the list has %d node(s)\n", cnt);
	traverse_list(p_head);

	return 0;
}

PNODE create_list(int len)
{
	int i;
	int val = 0;
	PNODE p_head, p_tail, p_new;

	p_head = (PNODE)malloc(sizeof(NODE));
	if(NULL == p_head)
	{
		printf("malloc failure!\n");
		exit(-1);
	}
	p_tail = p_head;
	p_tail->next = NULL;

	for(i = 0; i<len; i++)
	{
		printf("pls input %d(st) data:",i+1);
		scanf("%d", &val);
		p_new = (PNODE)malloc(sizeof(NODE));
		if(NULL == p_new)
		{
			printf("malloc failure!\n");
			exit(-1);
		}
		p_new->data = val;
		p_tail->next = p_new;
		p_new->next = NULL;
		p_tail = p_new;
	}

	return p_head;
}

traverse_list(PNODE p_head)
{
   PNODE p = p_head->next;

   while(NULL != p)
   {
		printf("%d ", p->data);
		p = p->next;
   }
   printf("\n");
}

bool empty_list(PNODE p_head)
{
	if(NULL == p_head->next)
		return true;
	else 
		return false;
}

int get_list_node(PNODE p_head)
{
	int cnt = 0;
	PNODE p = p_head->next;
	while(NULL != p)
	{
		cnt++;
		p = p->next;
	}
	return cnt;
}